package com.test.selenium.util;

import static org.junit.Assert.assertEquals;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Iterator;

import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.junit.Ignore;
import org.junit.Test;

public class ReadWriteFiles {

	@Ignore
	@Test 
	public void testExcelReadAndWrite (){

		 FileInputStream file;
		try {
//			file = new FileInputStream(new File("../selenium/src/test/test.xls"));
//			HSSFWorkbook workbookIn = new HSSFWorkbook(file);
//			HSSFSheet sheetIn = workbookIn.getSheetAt(0);
//			Iterator<Row> rowIterator = sheetIn.iterator();
//			
//	
//			Row row = rowIterator.next();
//			Iterator<Cell> cellIterator = row.cellIterator();
//			Cell cell = cellIterator.next();
//			if (cell.getCellType() == Cell.CELL_TYPE_STRING) {
//				System.out.print(cell.getStringCellValue());
//				assertEquals(cell.getStringCellValue().trim(), "Username");	
//			}
			
			HSSFWorkbook workbookOut = new HSSFWorkbook();
			HSSFSheet sheetOut = workbookOut.createSheet("Air Result");
			Row rowOut = sheetOut.createRow(0);
			//Create a new cell in current row
			Cell cellOut = rowOut.createCell(0);
			//Set value to new value
			cellOut.setCellValue("Blahblah");
			FileOutputStream out = new FileOutputStream(new File("../selenium/src/test/test.xls"));
		    workbookOut.write(out);
//		    out.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	 }
	
	@Test
	@Ignore
	public void testRewrite() {
		try {
			HSSFWorkbook workbookOut = new HSSFWorkbook(new FileInputStream(new File("../selenium/src/test/test.xls")));
			HSSFSheet sheetOut = workbookOut.getSheet("Air Result");
			Row rowOut = sheetOut.createRow(1);
			//Create a new cell in current row
			Cell cellOut = rowOut.createCell(0);
			//Set value to new value
			cellOut.setCellValue("newString");
			FileOutputStream out = new FileOutputStream(new File("../selenium/src/test/test.xls"));
		    workbookOut.write(out);
//		    out.close();
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	
	@Test
	@Ignore
	public void testTemplate() throws FileNotFoundException, IOException {
		HSSFWorkbook workbookOut = new HSSFWorkbook(new FileInputStream(new File("../selenium/src/Template/SmokeTest_Output_Template.xls")));
		HSSFSheet sheetOut = workbookOut.getSheet("Test Results");
		String str = sheetOut.getRow(0).getCell(0).getStringCellValue();
		System.out.println(str);
		
		Row row1 = sheetOut.createRow(1);
		row1.createCell(0).setCellValue("foo");
		row1.createCell(1).setCellValue("pass");
		
		String outFileName = "..\\selenium\\src\\Output\\SmokeTest-AirModeResults.xls";
		File outFile = new File(outFileName);
		FileOutputStream outStream = new FileOutputStream(outFile);
		workbookOut.write(outStream);
	}
}

